package com.tracy.lifeproject.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.tracy.lifeproject.R;
import com.tracy.lifeproject.activity.SearchActivity;
import com.tracy.lifeproject.discovery.adapter.DiscoveryViewPagerAdapter;
import com.tracy.lifeproject.discovery.fragment.StrategyFragment;
import com.tracy.lifeproject.discovery.goods.fragment.GoodsFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * A simple {@link Fragment} subclass.
 * 发现 搜索界面的碎片
 */
public class DiscoveryFragment extends Fragment {


    @InjectView(R.id.toolbar_discovery)
    Toolbar toolbarDiscovery;
    @InjectView(R.id.viewPager_discovery)
    ViewPager viewPagerDiscovery;
    @InjectView(R.id.tab_discovery)
    TabLayout tabDiscovery;

    private List<Fragment> mFragmentList;
    private DiscoveryViewPagerAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new DiscoveryViewPagerAdapter(getChildFragmentManager());
        initFragment();//初始化碎片
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discovery, container, false);
        ButterKnife.inject(this, view);
        //给Viewpager设置适配器
        viewPagerDiscovery.setAdapter(adapter);
        tabDiscovery.setupWithViewPager(viewPagerDiscovery);
        //顶部搜索跳转
        toolbarDiscovery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    /**
     * 初始化碎片
     */
    private void initFragment() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new StrategyFragment());
        mFragmentList.add(new GoodsFragment());
        //给适配器设置数据
        adapter.setDatas(mFragmentList);
    }
}
